<template>
  <div id="recruitlist">
    <!-- 新增标准 -->
    <el-dialog title="新增标准" :visible.sync="editFormVisible" width="600px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm" label-width="100px" ref="editForm" :inline="true" style="padding:10px 0 0;clear:both" inline-message :rules="editFormRules">
        <el-form-item prop="theYear" label="年度">
          <el-date-picker
            v-model="editForm.theYear"
            type="year"
            placeholder="请选择年度"
            style="width:260px"
            value-format="yyyy"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="standardType" label="标准类型">
          <el-input v-model="editForm.standardType" style="width:260px" placeholder="请输入标准类型" />
        </el-form-item>
        <div>
          <el-form-item prop="fileId" label="标准模版">
            <el-upload
              ref="upload"
              :action="this.$http.defaults.baseURL+this.$http.defaults.salaryAPI+'api/salaryGenFileInfo/upload'"
              :on-success="uploadSuccess"
              :on-error="uploadError"
              :before-upload="beforeUpload"
              :on-remove="handleRemove"
              :file-list="fileList"
              accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            >
              <el-button type="primary" size="small" :loading="uploadloading">上传文件</el-button>
            </el-upload>
          </el-form-item>
        </div>
        <el-form-item label=" ">
          <el-button type="primary" @click.native="editFormSubmit" :loading="saveloading">保存</el-button>
          <el-button @click.native="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 面包屑 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/salary' }">工资管理</el-breadcrumb-item>
      <el-breadcrumb-item>标准维护</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 工具条 -->
    <section class="wboxs">
      <div style="display: flex; justify-content: end;">
        <el-button icon="el-icon-download" type="primary" @click="downloadTemplate" :loading="btnLoading">下载模版</el-button>
        <el-button icon="el-icon-plus" type="success" @click="openEditForm" :loading="btnLoading">新增</el-button>
      </div>
    </section>
    <!-- 标准列表 -->
    <section class="wboxd" style="margin-top:10px">
      <el-table :data="standardList" highlight-current-row v-loading="listLoading" style="width: 100%;border-top:1px solid #9CA3B3" :height="tableHeight" border id="selectionindexlist" row-key="personId"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="年度" align="center" prop="theYear" width="200" />
        <el-table-column prop="isCurrent" label="是否当前标准" align="center">
          <template slot-scope="scope">
            {{scope.row.isCurrent===1?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="标准类型" align="center" prop="standardType">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <template>
              <el-button type="success" size="small" v-if="!scope.row.isCurrent" @click="handleSetCurrent(scope.row)" :loading="btnLoading">设为当前标准</el-button>
              <el-button type="danger" size="small" @click="handleDel(scope.row.id)" :loading="btnLoading">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
export default {
  data () {
    return {
      editFormVisible: false,
      editForm: {
        id: null,
        fileId: '',
        isCurrent: 1,
        standardType: '',
        theYear: ''
      },
      fileList: [],
      editFormRules: {
        theYear: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        standardType: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        fileId: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      },
      listLoading: false,
      standardList: [],
      uploadloading: false,
      saveloading: false,
      tableHeight: 100,
      btnLoading: false
    }
  },
  methods: {
    // 下载模版
    downloadTemplate () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.salaryAPI + 'api/salaryBusStandard/getTemplate')
    },
    // 获取标准列表
    getList () {
      this.listLoading = true
      let para = {
        pageNum: 1,
        pageSize: 1000
      }
      this.$http.post(this.$http.defaults.baseURL + this.$http.defaults.salaryAPI + 'api/salaryBusStandard/queryByPage', para)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            this.standardList = response.data.result.records
          } else {
            console.log('获取数据失败:' + response.data.message)
            this.$message({
              message: '获取数据失败:' + response.data.message,
              type: 'error'
            })
          }
        }).catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
          this.listLoading = false
        })
    },
    // 打开新增弹层
    openEditForm () {
      this.editFormVisible = true
      this.editForm.id = null
      this.editForm.fileId = ''
      this.editForm.isCurrent = 1
      this.editForm.standardType = ''
      this.editForm.theYear = ''
    },
    // 新增标准
    editFormSubmit () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          let para = { ...this.editForm }
          console.log(para)
          this.saveloading = true
          this.$http.post(this.$http.defaults.baseURL + this.$http.defaults.salaryAPI + `api/salaryBusStandard/save`, para)
            .then(response => {
              if (response.data.success === true) {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                })
                this.saveloading = false
                this.editFormVisible = false
                this.fileList = []
                this.getList()
              } else {
                console.log('新增失败:' + response.data.message)
                this.$message({
                  message: '新增失败:' + response.data.message,
                  type: 'error'
                })
                this.saveloading = false
              }
            }).catch(error => {
              console.log(error)
              this.$message({
                message: '新增失败:' + error,
                type: 'error'
              })
              this.saveloading = false
            })
        }
      })
    },
    // 删除标准
    handleDel (id) {
      this.$confirm('确认删除该标准吗？', '提示', {
        type: 'warning'
      }).then(() => {
        let para = {ids: [id]}
        this.btnLoading = true
        this.$http.post(this.$http.defaults.baseURL + this.$http.defaults.salaryAPI + 'api/salaryBusStandard/remove', para).then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.btnLoading = false
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
          .catch(error => {
            this.btnLoading = false
            console.log(error)
            this.$message({
              message: '删除失败:' + error,
              type: 'error'
            })
          })
      })
    },
    // 设为当前标准
    handleSetCurrent (row) {
      this.btnLoading = true
      let para = { ...row }
      para.isCurrent = 1
      this.$http.post(this.$http.defaults.baseURL + this.$http.defaults.salaryAPI + `api/salaryBusStandard/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          this.btnLoading = false
          this.getList()
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
          this.btnLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '设置失败:' + error,
          type: 'error'
        })
        this.btnLoading = false
      })
    },
    beforeUpload (file) {
      const isFormat = file.name.indexOf('.xls') > 0 || file.name.indexOf('.xlsx') > 0 || file.name.indexOf('.XLS') > 0 || file.name.indexOf('.XLSX') > 0
      if (!isFormat) {
        this.$message.error('附件格式必须为excel!')
      } else {
        this.uploadloading = true
      }
      return isFormat
    },
    uploadError (err) {
      console.log(err)
      this.uploadloading = false
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    uploadSuccess (res) {
      this.editForm.fileId = res.result[0].id
      this.uploadloading = false
      this.$message({
        message: '上传成功',
        type: 'success'
      })
    },
    handleRemove () {
      this.editForm.fileId = ''
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 222
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
.selectioncontent{
  border-bottom:1px solid #333;
  padding:5px !important;
}
  .pointtab .el-badge__content.is-fixed{
    right: -5px;
    top:18px;
  }
  .pointtab{
    padding-right:22px;
  }
  .pointRadio .el-badge__content.is-fixed{
    right: -5px;
    top:6px;
  }
  .pointRadio .el-radio-button__inner{
    height:40px;
    padding-left:15px;
    padding-right:42px;
  }
#recruitlist .el-table--border td, .el-table--border th, .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed{
  border-bottom:1px solid #95B8E7;
}
#recruitlist .el-table td, #recruitlist .el-table th.is-leaf{
  border-right:1px solid #95B8E7;
}
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    text-align: center;
    border-right: #95B8E7 solid 1px;
    border-bottom: #95B8E7 solid 1px;
    min-height: 30px;
    line-height:30px;
  }
  .wboxd .el-table td{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体',
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #recruitlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #recruitlist .el-table__row{
    height: 50px;
  }
  #recruitlist #legent{
    float:right;
    margin-right: 20px;
  }
  #recruitlist #legent div{
    float:left;
    line-height: 42px;
    font-size:var(--size12);
  }
  #recruitlist #legent i{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: left;
    display: block;
    margin:5px 0px 0 3px;
  }
  #recruitlist #selectionindexlist .cell i.custom{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: center;
    display: block;
    margin:0 auto;
  }
  #recruitlist .el-step__title.is-finish {
    color: #409EFF;
  }
  #recruitlist .el-step__title {
    font-size:12px;
    line-height: 22px;
    padding-left: 8px;
  }
  #recruitlist .el-steps{
    margin:10px 0;
  }
  .el-tooltip__popper.is-dark{
    font-size:var(--size18) !important;
  }
  .el-tabs--card>.el-tabs__header {
    border-bottom: 1px solid #9CA3B3;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__nav {
    border: 1px solid #9CA3B3;
    border-bottom: 0;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__item {
    border-left: 1px solid #9CA3B3;
    /* background-color: #F0F0F0; */
  }
  .el-tabs__item.is-active {
    color: #0000FF !important;
    font-weight: bolder !important;
    /* background-color: #fff !important; */
    /* border-bottom: 2px solid #9CA3B3 !important; */
  }
</style>
<style scoped>
.mytable{
  border-top: 1px solid #74a6cc;
  border-left: 1px solid #74a6cc;
  margin:0 10px 10px;
  width: calc(100% - 20px);
}
.mytable th{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
  background: #FAFAFA;
  width:250px;
  text-align:right;
}
.mytable td{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
}
  .el-tooltip__popper{
    font-size:var(--size) !important;
  }
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
